package com.hy.common.utils;

import com.hy.entity.pojo.HyProperty;
import com.hy.entity.pojo.HyPropertySort;
import com.hy.entity.vo.HyUserVO;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.security.core.context.SecurityContextHolder;

import java.text.DecimalFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class CommonUtils {

    public static Double formatDouble(double number, int decimalPlaces) {
        String formatString = "%." + decimalPlaces + "f";
        return Double.valueOf(String.format(formatString, number));
    }
    public static String getStringAfterDash(String str) {
        int dashIndex = str.indexOf("-");
        if (dashIndex != -1 && dashIndex < str.length() - 1) {
            return str.substring(dashIndex + 1);
        }
        return "";
    }
    public static   String getEPC(HyProperty property){


        String unitId = property.getUnitId().toString();
        String propertyRfid =property.getPropertyRfid().toString();
        Integer ids=Integer.parseInt(unitId+propertyRfid);
        return ExcelUtil.Bytes2HexString(ids);

    }
    public  static String getPropertyCode(HyProperty property, HyPropertySort sort){

        String hexadecimal = Integer.toHexString(property.getPropertyId()).toUpperCase();
        String firstLetter = getFirstLetter(sort.getSortName()).toUpperCase();
        String code=sort.getCode();

        return new StringBuilder(firstLetter).append("-").append(hexadecimal).append("-").append(code).toString();

    }

    public static String getFirstLetter(String chinese) {
        StringBuilder firstLetter = new StringBuilder();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        for (char c : chinese.toCharArray()) {
            try {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if (pinyinArray != null && pinyinArray.length > 0) {
                    firstLetter.append(pinyinArray[0].charAt(0));
                } else {
                    firstLetter.append(c);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                firstLetter.append(c);
            }
        }

        return firstLetter.toString();
    }
    public static HyUserVO getCurrentUser(){


        return (HyUserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    public static boolean isMember(List<String> roles, String role){
        Iterator<String> iterator = roles.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            if (next.equals(role)){
                return true;
            }
        }
        return false;

    }
    public static boolean isMember(List<String> roles,String...role){
        Iterator<String> iterator = roles.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            for (String roleLeng:role) {
                if (next.equals(roleLeng)){
                    return true;
                }
            }

        }
        return false;

    }
    public static String getPermission(List<String> roles){
        StringBuilder stringBuilder=new StringBuilder();
        Iterator<String> iterator = roles.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            stringBuilder.append(next);
            stringBuilder.append(",");
        }
        stringBuilder.deleteCharAt(stringBuilder.length()-1);
        return stringBuilder.toString();

    }
    public static String getDate(){
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowStr = localDateTime.format(format);
        System.out.println(nowStr);
        return nowStr;

    }
}
